package com.fenghongzhang;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.fenghongzhang.day008.R;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class ImageActivity extends AppCompatActivity {
    private Button down;
    private Button read;
    private ImageView img;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image);


        down = (Button) findViewById(R.id.down);
        read = (Button) findViewById(R.id.read);
        img = (ImageView) findViewById(R.id.img);


        read.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String externalStorageState = Environment.getExternalStorageState();
                if(externalStorageState.equals(Environment.MEDIA_MOUNTED)) {//
                    try {
                        FileInputStream fileInputStream = new FileInputStream(new File(Environment.getExternalStorageDirectory(), "one.jpg"));
//
//                        //两种方式
//                        //bitmap 1.
//                        Bitmap bitmap = BitmapFactory.decodeStream(fileInputStream);
//                        img.setImageBitmap(bitmap);

                        //2.
                        File file = new File(Environment.getExternalStorageDirectory(), "one.jpg");
                        String absolutePath = file.getAbsolutePath();
//                        Toast.makeText(ImageActivity.this, absolutePath, Toast.LENGTH_SHORT).show();
                        Bitmap bitmap = BitmapFactory.decodeFile(absolutePath);
                        img.setImageBitmap(bitmap);
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    }
                }
            }
        });

        down.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            URL url = new URL("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1597815488173&di=00dbc2bccac92efbd3f34903716ee578&imgtype=0&src=http%3A%2F%2Fa2.att.hudong.com%2F36%2F48%2F19300001357258133412489354717.jpg");
                            HttpURLConnection connection = (HttpURLConnection) url.openConnection();

                            if(connection.getResponseCode() == 200){
                                InputStream inputStream = connection.getInputStream();

                                final Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        img.setImageBitmap(bitmap);
                                    }
                                });

//                                String externalStorageState = Environment.getExternalStorageState();
//                                if(externalStorageState.equals(Environment.MEDIA_MOUNTED)){//挂载1
//                                    File file = Environment.getExternalStorageDirectory();
//                                    FileOutputStream fileOutputStream = new FileOutputStream(new File(file, "one.jpg"));
//
//                                    byte[] bytes = new byte[1024];
//                                    int len = 0;
//                                    while((len = inputStream.read(bytes)) != -1){
//                                        fileOutputStream.write(bytes,0,len);
//                                    }
//
//                                    fileOutputStream.close();
//                                    inputStream.close();
//
//                                }
                            }

                        } catch (MalformedURLException e) {
                            e.printStackTrace();
                        } catch (IOException e) {
                        }

                    }
                }).start();



            }
        });


    }
}
